package com.doit.day01;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.List;

public class NameNodeDemo {

    public void listenDataNodes() throws Exception {
        ZooKeeper zooKeeper = new ZooKeeper("linux01:2181", 2000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("namenode 上线了，开始正常工作了.....");
            }
        });

        //如果说我的datanodes节点中有变化了，要重新获取列表里面所有的节点
        zooKeeper.getChildren("/datanodes", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                try {
                    List<String> children = zooKeeper.getChildren("/datanodes", this);
                    System.out.println("新的节点是："+children);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        //这个休眠是必须得，因为要不断地去监听
        Thread.sleep(Long.MAX_VALUE);
    }
    public static void main(String[] args) throws Exception {
        new NameNodeDemo().listenDataNodes();
    }
}
